{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "52e48b30",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "summarize mnist\n",
      "['log_mnist_saliency_K64_chunk1.txt']\n",
      "['log_mnist_saliency_K64_chunk1.txt' 'log_mnist_saliency_K96_chunk1.txt']\n",
      "['log_mnist_saliency_K64_chunk1.txt' 'log_mnist_saliency_K96_chunk1.txt'\n",
      " 'log_mnist_saliency_K160_chunk1.txt']\n",
      "['log_mnist_saliency_K64_chunk1.txt' 'log_mnist_saliency_K96_chunk1.txt'\n",
      " 'log_mnist_saliency_K160_chunk1.txt' 'log_mnist_saliency_K320_chunk1.txt']\n",
      "['log_mnist_saliency_K64_chunk1.txt' 'log_mnist_saliency_K96_chunk1.txt'\n",
      " 'log_mnist_saliency_K160_chunk1.txt' 'log_mnist_saliency_K320_chunk1.txt'\n",
      " 'log_mnist_saliency_K16_chunk2.txt']\n",
      "['log_mnist_saliency_K64_chunk1.txt' 'log_mnist_saliency_K96_chunk1.txt'\n",
      " 'log_mnist_saliency_K160_chunk1.txt' 'log_mnist_saliency_K320_chunk1.txt'\n",
      " 'log_mnist_saliency_K16_chunk2.txt' 'log_mnist_saliency_K24_chunk2.txt']\n",
      "['log_mnist_saliency_K64_chunk1.txt' 'log_mnist_saliency_K96_chunk1.txt'\n",
      " 'log_mnist_saliency_K160_chunk1.txt' 'log_mnist_saliency_K320_chunk1.txt'\n",
      " 'log_mnist_saliency_K16_chunk2.txt' 'log_mnist_saliency_K24_chunk2.txt'\n",
      " 'log_mnist_saliency_K40_chunk2.txt']\n",
      "['log_mnist_saliency_K64_chunk1.txt' 'log_mnist_saliency_K96_chunk1.txt'\n",
      " 'log_mnist_saliency_K160_chunk1.txt' 'log_mnist_saliency_K320_chunk1.txt'\n",
      " 'log_mnist_saliency_K16_chunk2.txt' 'log_mnist_saliency_K24_chunk2.txt'\n",
      " 'log_mnist_saliency_K40_chunk2.txt' 'log_mnist_saliency_K80_chunk2.txt']\n",
      "['log_mnist_saliency_K64_chunk1.txt' 'log_mnist_saliency_K96_chunk1.txt'\n",
      " 'log_mnist_saliency_K160_chunk1.txt' 'log_mnist_saliency_K320_chunk1.txt'\n",
      " 'log_mnist_saliency_K16_chunk2.txt' 'log_mnist_saliency_K24_chunk2.txt'\n",
      " 'log_mnist_saliency_K40_chunk2.txt' 'log_mnist_saliency_K80_chunk2.txt'\n",
      " 'log_mnist_saliency_K4_chunk4.txt']\n",
      "['log_mnist_saliency_K64_chunk1.txt' 'log_mnist_saliency_K96_chunk1.txt'\n",
      " 'log_mnist_saliency_K160_chunk1.txt' 'log_mnist_saliency_K320_chunk1.txt'\n",
      " 'log_mnist_saliency_K16_chunk2.txt' 'log_mnist_saliency_K24_chunk2.txt'\n",
      " 'log_mnist_saliency_K40_chunk2.txt' 'log_mnist_saliency_K80_chunk2.txt'\n",
      " 'log_mnist_saliency_K4_chunk4.txt' 'log_mnist_saliency_K6_chunk4.txt']\n",
      "['log_mnist_saliency_K64_chunk1.txt' 'log_mnist_saliency_K96_chunk1.txt'\n",
      " 'log_mnist_saliency_K160_chunk1.txt' 'log_mnist_saliency_K320_chunk1.txt'\n",
      " 'log_mnist_saliency_K16_chunk2.txt' 'log_mnist_saliency_K24_chunk2.txt'\n",
      " 'log_mnist_saliency_K40_chunk2.txt' 'log_mnist_saliency_K80_chunk2.txt'\n",
      " 'log_mnist_saliency_K4_chunk4.txt' 'log_mnist_saliency_K6_chunk4.txt'\n",
      " 'log_mnist_saliency_K10_chunk4.txt']\n",
      "['log_mnist_saliency_K64_chunk1.txt' 'log_mnist_saliency_K96_chunk1.txt'\n",
      " 'log_mnist_saliency_K160_chunk1.txt' 'log_mnist_saliency_K320_chunk1.txt'\n",
      " 'log_mnist_saliency_K16_chunk2.txt' 'log_mnist_saliency_K24_chunk2.txt'\n",
      " 'log_mnist_saliency_K40_chunk2.txt' 'log_mnist_saliency_K80_chunk2.txt'\n",
      " 'log_mnist_saliency_K4_chunk4.txt' 'log_mnist_saliency_K6_chunk4.txt'\n",
      " 'log_mnist_saliency_K10_chunk4.txt' 'log_mnist_saliency_K20_chunk4.txt']\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/env python3\n",
    "# -*- coding: utf-8 -*-\n",
    "\"\"\"\n",
    "Created on Tue Dec 18 11:01:33 2018\n",
    "\n",
    "@author: seojin.bang\n",
    "\"\"\"\n",
    "import argparse\n",
    "import numpy as np\n",
    "import re\n",
    "\n",
    "def main(args):\n",
    "    \n",
    "    dataset = args.dataset\n",
    "    blackbox = args.blackbox\n",
    "    method = args.method\n",
    "    result_dir = args.result_dir\n",
    "    \n",
    "    measures = ['avg_acc', 'avg_acc_fixed', 'acc_zeropadded', \n",
    "                'vmi', 'vmi_fixed', 'vmi_zeropadded', \n",
    "                'precision_macro_approx', 'precision_macro_approx_fixed', 'precision_macro_zeropadded', \n",
    "                'recall_micro_approx', 'recall_micro_approx_fixed', 'recall_micro_zeropadded', \n",
    "                'f1_macro_approx', 'f1_macro_approx_fixed', 'f1_macro_zeropadded', \n",
    "                'precision_micro_approx', 'precision_micro_approx_fixed', 'precision_micro_zeropadded', \n",
    "                'recall_macro_approx', 'recall_macro_approx_fixed', 'recall_macro_zeropadded',\n",
    "                'f1_micro_approx', 'f1_micro_approx_fixed', 'f1_micro_zeropadded']\n",
    "\n",
    "    measures_order = ['acc_zeropadded', 'avg_acc', 'avg_acc_fixed', \n",
    "                'precision_macro_zeropadded', 'precision_macro_approx', 'precision_macro_approx_fixed', \n",
    "                'precision_micro_zeropadded', 'precision_micro_approx', 'precision_micro_approx_fixed', \n",
    "                'recall_macro_zeropadded', 'recall_macro_approx', 'recall_macro_approx_fixed',\n",
    "                'recall_micro_zeropadded', 'recall_micro_approx', 'recall_micro_approx_fixed',\n",
    "                'f1_macro_zeropadded', 'f1_macro_approx', 'f1_macro_approx_fixed',\n",
    "                'f1_micro_zeropadded', 'f1_micro_approx', 'f1_micro_approx_fixed',\n",
    "                'vmi', 'vmi_fixed', 'vmi_zeropadded']\n",
    "    \n",
    "    if dataset == 'mnist':\n",
    "        print('summarize mnist')\n",
    "        K = [64, 96, 160, 320, 16, 24, 40, 80, 4, 6, 10, 20]\n",
    "        chunksize = [1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4]\n",
    "        assert len(K) == len(chunksize)\n",
    "    \n",
    "        fns = np.array([])\n",
    "        for i in range(len(K)):\n",
    "            fns = np.append(fns, ['log_' + dataset + '_' + method + '_K' + str(K[i]) + '_chunk' + str(chunksize[i]) + '.txt'])\n",
    "    \n",
    "    for mea_idx in range(len(measures)):\n",
    "        for fn in fns:\n",
    "            #f = open(Path(result_dir).joinpath(fn), 'r')\n",
    "            f = open(result_dir + '/' + fn, 'r')\n",
    "            if f.mode == 'r':\n",
    "                contents = f.read()\n",
    "                idx = contents.find(measures[mea_idx])\n",
    "                if measures[mea_idx] is not measures_order[len(measures_order)-1]:\n",
    "                    measures_next = measures_order[measures_order.index(measures[mea_idx]) + 1]\n",
    "                    idx_next = contents.find(measures_next)\n",
    "                else:\n",
    "                    idx_next = len(contents)\n",
    "                val = contents[(idx + len(measures[mea_idx]) + 1):(idx_next - 1)]\n",
    "                val = re.sub('[a-zA-Z\\n]', '', val)\n",
    "                print(val)\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    \n",
    "    parser = argparse.ArgumentParser()\n",
    "    \n",
    "    parser.add_argument('--dataset', default='mnist', type=str, help='dataset name: imdb-sent, imdb-word, mnist, mimic')\n",
    "    parser.add_argument('--result_dir', default='./result', type=str, help='Result directory path')\n",
    "    parser.add_argument('--blackbox', default='cnn4', type=str, help='blackbox model type')\n",
    "    parser.add_argument('--method', default='saliency', type=str, help='interpretable learning method')\n",
    "\n",
    "    args = parser.parse_args([])\n",
    "    \n",
    "    main(args)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22a0a0e3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
